package com.smilive.delay.queue.rocketmq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @program: delay_queue_start
 * @Date: 2022-02-17 14:28
 * @Author: smilive
 * @Description:
 */
@Component
@Slf4j
public class RocketMqCustomer {

    private static final DateTimeFormatter F = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    // topic需要和生产者的topic一致，consumerGroup属性是必须指定的，内容可以随意
    // selectorExpression的意思指的就是tag，默认为“*”，不设置的话会监听所有消息
    @Service
    @RocketMQMessageListener(topic = RocketMqProducer.TOPIC, selectorExpression = "*", consumerGroup = "TEST_CONSUMER_1")
    public class ConsumerSend implements RocketMQListener<RocketmqDelayTask> {
        // 监听到消息就会执行此方法
        @Override
        public void onMessage(RocketmqDelayTask task) {
            log.info("任务执行时间:{},任务创建时间:{},任务ID:{}",
                    LocalDateTime.now().format(F),
                    LocalDateTime.ofInstant(Instant.ofEpochMilli(task.getTimestamp()), ZoneId.systemDefault()).format(F),
                    task.getTaskId());
        }
    }
}
